package kpi.java_training.testing.db;

import kpi.java_training.testing.user.User;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * The CSVParser class provides..
 *
 * @author Alexander Palleev
 * @author palleev@gmail.com
 */
public class CSVParser {
    //    private String csvFile;
    private String csvSplitBy = ";";
    private final String DEFAULT_USERS_FILE = "users.txt";
    private final String DEFAULT_STATISTICS_FILE = "statistics.txt";

    private ArrayList<String> statisticsArray;

    public CSVParser() {
        statisticsArray = new ArrayList<String>();
    }

    public User authenticateUser(String name, String password) {
        User user = null;
        BufferedReader br = null;
        String line = "";

        try {
            br = new BufferedReader(new FileReader(DEFAULT_USERS_FILE));

            while ((line = br.readLine()) != null) {
                // use csvSplitBy as separator
                String[] userData = convertStringToData(line);

                if (name.equals(userData[0]) && password.equals(userData[1])) {
                    user = new User(userData[0], userData[1], userData[2]);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return user;
    }

    public ArrayList<String> getStatistics() {
        statisticsArray = new ArrayList<String>();
        BufferedReader bufferedReader = null;
        String line = "";

        try {
            File file = new File(DEFAULT_STATISTICS_FILE);

            if (file.exists()) {
                bufferedReader = new BufferedReader(new FileReader(DEFAULT_STATISTICS_FILE));

                while ((line = bufferedReader.readLine()) != null) {
                    String[] statisticsData = convertStringToData(line);
                        statisticsArray.add(line);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return statisticsArray;
    }

    public void setStatistics(String userName, int questions, int correctAnswers) {
        BufferedWriter bufferedWriter = null;
        FileWriter fileWriter;
        String line = "";

        try {
            File file = new File(DEFAULT_STATISTICS_FILE);

            DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
            Date today = Calendar.getInstance().getTime();
            String testDate = df.format(today);

            String currentUserStatistics = String.format("%s;%s;%s;%s", userName, testDate, questions, correctAnswers);
            statisticsArray.add(currentUserStatistics);

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

           /* for (int index = 0; index < statisticsArray.size(); index++){
                String oneUserStatistics = statisticsArray.get(index);
                String[] statisticsData = convertStringToData(oneUserStatistics);
                if (statisticsData[0].equals(userName)){
                    statisticsArray.add(index, currentUserStatistics);
                }
            }*/

            fileWriter = new FileWriter(file.getAbsoluteFile());
            bufferedWriter = new BufferedWriter(fileWriter);

            for (String userStatistics : statisticsArray){
                bufferedWriter.write(userStatistics);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // convert question text to data array
    private String[] convertStringToData(String line) {
        String lineText = line;
        String[] lineData = lineText.split(csvSplitBy);

        return lineData;
    }
}
